package com.api.dao;

import com.api.common.RestResponse;
import com.api.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;

import javax.xml.ws.Action;
import java.util.List;

@Repository
public class UserDao {

//    @Autowired
//    private GenericRest rest;


    private String userServiceName="LOGINANDREGISTER";

    @Autowired
    private RestTemplate restTemplate;

    public User login(User query) {

        return restTemplate.postForObject("http://" + userServiceName + "/login",query,User.class);
    }
/*    @Autowired
    private GenericRest rest;
    @Value("${user.service.name}")
    private String userServiceName;*/


    /*public String getusername(Long id){
        String url="http://user/user/getusername?id="+id;
        RestResponse<String> response = rest.get(url, new ParameterizedTypeReference<RestResponse<String>>() {
        }).getBody();
        return response.getResult();
    }

    public List<User> getUserList(User query) {
        ResponseEntity<RestResponse<List<User>>> resutlEntity = rest.post("http://" + userServiceName + "/user/getList", query, new ParameterizedTypeReference<RestResponse<List<User>>>() {
        });
        RestResponse<List<User>> restResponse = resutlEntity.getBody();
        //判断状态码
        if(restResponse.getCode()==0){
            return  restResponse.getResult();
        }else{
            return null;
        }

    }

    public User addUser(User account) {
        String url="http://"+userServiceName+"/user/add";
        ResponseEntity<RestResponse<User>> responseEntity = rest.post(url, account, new ParameterizedTypeReference<RestResponse<User>>() {
        });
        RestResponse<User> restResponse = responseEntity.getBody();
        //判断状态码
        if(restResponse.getCode()==0){
            return  restResponse.getResult();
        }else{
            return null;
        }
    }*/
}
